package cn.net.vote.data.aspect;

import cn.net.vote.data.annotation.SysLog;
import cn.net.vote.data.domain.PageData;
import cn.net.vote.data.pojo.log.SysLogEntity;
import cn.net.vote.util.HttpContextUtils;
import cn.net.vote.util.IPUtils;

import com.google.gson.Gson;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Method;

/**
 * 【描 述】：系统日志，切面处理类
 * 【环 境】：J2SE 1.8
 * @author   Tianxy
 * @version  version 1.0
 * @since    2018年1月8日
 */
@Aspect
@Component
public class SysLogAspect {

	
	@Pointcut("@annotation(cn.net.vote.data.annotation.SysLog)")
	public void logPointCut() { 
		
	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		//执行方法
		Object result = point.proceed();
		//执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;

		//保存日志
		saveSysLog(point, time);

		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		SysLogEntity sysLog = new SysLogEntity();
		PageData pd = new PageData();
		SysLog syslog = method.getAnnotation(SysLog.class);
		if(syslog != null){
			//注解上的描述
			String value = syslog.value();
			String[] split = value.split("-");
			String action = split[0];
			String type = split[1];
			sysLog.setAction(action);
			sysLog.setType(type);
		} 

		//请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");

		//请求的参数
		Object[] args = joinPoint.getArgs();
		try{
			if(args.length == 2){
				String token = (String) args[0];
//				Jws<Claims> parseJwt = JwtUtil.parseJwt(token);
//				Integer id = (Integer) parseJwt.getBody().get("id");
//				String name = (String) parseJwt.getBody().get("name");
				sysLog.setOpt_id(9999999);
				sysLog.setOpt_name("admin");
				String params = new Gson().toJson(args[1]);
				sysLog.setParams(params);
				
			} else {
				String token = (String) args[0];
//				Jws<Claims> parseJwt = JwtUtil.parseJwt(token);
//				Integer id = (Integer) parseJwt.getBody().get("id");
//				String name = (String) parseJwt.getBody().get("name");
				sysLog.setOpt_id(9999999);
				sysLog.setOpt_name("admin");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		//获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		//设置IP地址
		String ip = IPUtils.getIpAddr(request);
		sysLog.setIp(ip);
		sysLog.setTime(time);
		sysLog.setDetails(sysLog.getOpt_name()+"("+ip+") 访问:"+className + "." + methodName + "()"+" 传入:"+sysLog.getParams()+" 执行:"+sysLog.getAction()+sysLog.getType()+" 共用时:"+sysLog.getTime());
		//保存系统日志
//		logMapper.addLog(sysLog);
	}
}
